package com.exercise.widgets.searchable

import android.app.SearchManager
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.exercise.struct.base.BaseActivity
import com.exercise.struct.viewmodel.BaseViewModel
import com.exercise.widgets.R
import com.exercise.widgets.databinding.ActivitySearchableBinding

class SearchableActivity : BaseActivity<BaseViewModel, ActivitySearchableBinding>() {
    override val bindingInflater: (LayoutInflater) -> ActivitySearchableBinding
        get() = ActivitySearchableBinding::inflate
    override val modelClass: Class<BaseViewModel>
        get() = BaseViewModel::class.java

    override fun initData() {
    }

    override fun initView() {
        handleIntent(intent)
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        setIntent(intent)
        handleIntent(intent)
    }

    private fun handleIntent(intent: Intent) {
        val jargon = intent.getBundleExtra(SearchManager.APP_DATA)?.getBoolean("jargon")
        Log.d("handleIntent", "jargon: $jargon")
        if (Intent.ACTION_SEARCH == intent.action) {
            intent.getStringExtra(SearchManager.QUERY)?.also { query ->
                doMySearch(query)
            }
        }
    }

    private fun doMySearch(query: String) {
        Toast.makeText(this, "搜索中...$query", Toast.LENGTH_SHORT).show()
    }

}